/* $RCSFile: ADNDynUom.cs $
 * Apache license v2 2011 by Christian Surieux
 * $Source: ?/CRMWrapper3/DynEntities/ADNDynUom.cs $
 * $log$
 * Revision 19 2010/10/02 14:01:49  christian.surieux
 *   Corrected a bug in allmost everyADNDyn file for PrimaryFieldVualue
 * Revision 6 2010/08/06 19:56:33  christian.surieux
 *   Added Log to All cs files
 */

using System;
using System.Diagnostics;
using System.Collections;
using System.ComponentModel;
using System.Collections.Generic;
using System.Text;
using CrmSdk;

namespace AddonNice.CRMWrapper
{
	public class ADNDynUom : DynEntity
	{
        protected class Ftor: DFactor
        {
            public Ftor() : base(CrmStr.uom)
            {}

            public override Type DynType
            { get { return typeof(ADNDynUom); } }

            public override string DynTypename
            {   get{ return CrmStr.uom;}}

            public override DynEntity Build()
            {   return new ADNDynUom();}

            public override DynEntity Build(CRMWrapperBase w, InternalDynamicEntity dEntity, PropertyDescriptorCollection wproperties)
            {   return new ADNDynUom(w, dEntity, wproperties, null);}

            public override DynEntity Build(CRMWrapperBase w, string PrimaryFieldValue)
            {   return new ADNDynUom(w, PrimaryFieldValue);}

            public override DynEntity Build(CRMWrapperBase w,Guid PrimaryKeyValue)
            {   return new ADNDynUom(w,PrimaryKeyValue);}
        }

        static ADNDynUom(){new Ftor();} 

        public override bool HasOwnerProperty
        {
            get{   return false;}
        }

        public override bool HasOwninguser
        {
            get{   return false;}
        }

        public override bool HasBusinessUnitProperty
        {
            get{   return false;}
        }

        public override bool HasOwningBusinessUnitProperty
        {
            get{   return false;}
        }

        public override bool OwningbusinessunitAsUniqueIdentifier
        {
            get{   return false;}
        }

        public ADNDynUom(): base()
		{}

        public ADNDynUom(CRMWrapperBase w, string PrimaryFiedlValue)
			: base(w, CrmStr.uom)
		{
            if ( PrimaryFiedlValue != string.Empty )
                name    =   PrimaryFiedlValue;
		}
        public ADNDynUom(ResetWrapperHandler fn, string PrimaryFiedlValue)
			: base(fn, CrmStr.uom)
		{
            if ( PrimaryFiedlValue != string.Empty )
                name    =   PrimaryFiedlValue;
		}

		public ADNDynUom(CRMWrapperBase w, Guid CrmId)
			: base(w,CrmStr.uom,CrmId)
		{}
		public ADNDynUom(ResetWrapperHandler fn,Guid CrmId)
			: base(fn,CrmStr.uom,CrmId)
		{}

		public ADNDynUom(CRMWrapperBase w, InternalDynamicEntity e)
			: base(w,CrmStr.uom,e,null,null)
		{}
		public ADNDynUom(ResetWrapperHandler fn, InternalDynamicEntity e)
			: base(fn,CrmStr.uom,e,null,null)
		{}

		public ADNDynUom(CRMWrapperBase w, InternalDynamicEntity e,PropertyDescriptorCollection prop,InternalColumnSet s)
			: base(w,CrmStr.uom,e,prop,s)
		{}

		public ADNDynUom(ResetWrapperHandler fn,InternalDynamicEntity e,PropertyDescriptorCollection prop,InternalColumnSet s)
			: base(fn,CrmStr.uom,e,prop,s)
		{}

		/// <summary>
		/// Crm Key
		/// </summary>
        [CrmDynAttribute(CrmStr.uom)]
        public Guid uomid
		{
			get
			{
				return KeyPropertyValue(CrmStr.uomid);
			}
			set
			{
				AddKeyProperty(CrmStr.uomid, value);
			}
		}

        protected override void SetCrmIdProperty(Guid crmId)
        {
           if ( !dicProperty.ContainsKey(CrmStr.uomid) || KeyPropertyValue(CrmStr.uomid) != crmId )
                uomid   =   crmId;
        }
        
        public override string PrimaryFieldName
        {
            get
            {
               return CrmStr.name;
            }
           set
           {}
        }

        /// <summary>
        /// Value contained in the PrimaryField for the DynEntity or empty if no PrimaryField
        /// </summary>
        public override string PrimaryFieldStringValue
        {
            get
            {
                return name;
            }
            set
            {
                name =   value;
            }
        }

        public override Guid PrimaryKeyGuidValue
        {
           get
           {
                return uomid;
           }
           set
           {
               uomid    =   value;
           }
        }

        /// <summary>
        /// Gets a value that indicates whether this is the base unit for the unit group.
        /// </summary>
        [CrmDynAttribute(CrmStr.uom)]
        public bool isschedulebaseuom 
        {
            get 
            {
                return BooleanPropertyValue(CrmStr.isschedulebaseuom);
            }
            set
            {
                AddBooleanProperty(CrmStr.isschedulebaseuom,value);
            }
        }

        /// <summary>
        /// Gets or sets the ID of the base unit for the product.
        /// </summary>
        [CrmDynAttribute(CrmStr.uom)]
        public Guid baseuom
        {
            get
            {
                if (ContainsKey(CrmStr.baseuom))
                    return LookupPropertyValue(CrmStr.baseuom);
                return Guid.Empty;
            }
            set
            {
                AddLookupProperty(CrmStr.baseuom,CrmStr.uom,value);
            }
        }

        /// <summary>
        /// Gets or sets the ID of the unit group that is associated with the unit.
        /// </summary>
        [CrmDynAttribute(CrmStr.uom)]
        public Guid uomscheduleid
        {
            get
            {
                if (ContainsKey(CrmStr.uomscheduleid))
                    return LookupPropertyValue(CrmStr.uomscheduleid);
                return Guid.Empty;
            }
            set
            {
                AddLookupProperty(CrmStr.uomscheduleid,CrmStr.uomschedule,value);
            }
        }

        public string uomscheduleName
        {
            get
            {
                if (ContainsKey(CrmStr.uomscheduleid))
                    return LookupPropertyName(CrmStr.uomscheduleid);
                return string.Empty;
            }
        }

        /// <summary>
        /// Gets or sets the unit quantity for the product.
        /// </summary>
        [CrmDynAttribute(CrmStr.uom)]
        public decimal quantity
        {
            get
            {
                return MoneyPropertyValue(CrmStr.quantity);
            }
            set
            {
                AddMoneyProperty(CrmStr.quantity,value);
            }
        }
        
        // fin base unit name and quantity in base unit
        public decimal GetBaseQuantity(ref string uomName,ref Guid baseId)
        {
            bool isbase             =   isschedulebaseuom;
            bool hasParent          =   ContainsKey(CrmStr.baseuom);
            decimal q               =   quantity;
            uomName                 =   name;
            baseId                  =   uomid;
            while (!isbase && hasParent )
            {
                ADNDynUom parentUom =   new ADNDynUom(wrapper,baseuom);
                parentUom.Retrieve();
                isbase              =   parentUom.isschedulebaseuom;
                hasParent           =   parentUom.ContainsKey(CrmStr.baseuom);
                q                   =   q*parentUom.quantity;
                uomName             =   parentUom.name;
                baseId              =   parentUom.uomid;
            }
            return q;
        }
    }
}
